//
//  SearchResultSegViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/7/22.
//  Copyright © 2020 王洋. All rights reserved.
//

#import "SearchNaviBarView.h"

#import "XLPageViewController.h"

#import "SearchResultPackageViewController.h"
#import "SearchResultProductViewController.h"
#import "SearchResultCaseViewController.h"
#import "SearchResultStoreViewController.h"

#import "SearchResultPostViewController.h"
#import "SearchResultQAViewController.h"
#import "SearchResultTopicViewController.h"

#import "BaiKeSecondClassesItemVC.h"
#import "SearchResultSegViewController.h"

@interface SearchResultSegViewController ()<XLPageViewControllerDelegate,XLPageViewControllerDataSrouce,UITextFieldDelegate>

@property (nonatomic, strong) SearchNaviBarView *searchNavi;

@property (nonatomic, strong) XLPageViewController *pageViewController;


@end

@implementation SearchResultSegViewController

- (void)dealloc {
    NSLog(@"SearchResultSegViewController---dealloc");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    [self initView];
}

- (void)initView {

    self.navigationController.navigationBarHidden = YES;
    #if RT_INTERACTIVE_PUSH
        self.rt_navigationController.rt_enableInteractivePush = YES;
    #endif
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.searchNavi.searchTF.text = self.keyWord;
    [self.view addSubview:self.searchNavi];
    
    [self.searchNavi mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(kStatusBarHeight);
        make.height.mas_equalTo(NavBar_Height);
    }];
    
    [self initPageViewController];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    self.keyWord = textField.text;
    NSMutableDictionary *searchDic = @{}.mutableCopy;
    [searchDic setValue:textField.text forKey:@"keyWord"];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"searchNoti" object:nil userInfo:searchDic];

    return YES;
}

- (void)goBack {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initPageViewController {
    
    XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
    config.shadowLineColor = UIColorHex(0xFF4242);
    config.titleSelectedFont = [UIFont boldSystemFontOfSize:16];
    config.titleNormalFont = [UIFont systemFontOfSize:16];
    config.titleViewAlignment = XLPageTitleViewAlignmentCenter;
    config.showTitleInNavigationBar = false;
    config.titleViewStyle = XLPageTitleViewStyleBasic;
    config.separatorLineHidden = true;
    config.shadowLineAnimationType = XLPageShadowLineAnimationTypeZoom;
    config.shadowLineWidth = 8;
    config.shadowLineHeight = 3;
    config.titleSpace = 30;
    
    self.pageViewController = [[XLPageViewController alloc] initWithConfig:config];

    self.pageViewController.delegate = self;
    self.pageViewController.dataSource = self;
    [self addChildViewController:self.pageViewController];
    [self.view addSubview:self.pageViewController.view];
    
    self.pageViewController.selectedIndex = self.selectIndex;
    
    [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.searchNavi.mas_bottom);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}

#pragma mark -
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {

    switch (index) {
        case SearchResultSegType_store:{
            
            SearchResultStoreViewController *vc = [[SearchResultStoreViewController alloc] initWithKeyWord:self.keyWord];
            return vc;
            
        } break;
        case SearchResultSegType_package:{
            
            SearchResultPackageViewController *vc = [[SearchResultPackageViewController alloc] initWithKeyWord:self.keyWord];
            return vc;
            
        } break;
        case SearchResultSegType_case:{
            
            SearchResultCaseViewController *vc = [[SearchResultCaseViewController alloc] initWithKeyWord:self.keyWord];
            return vc;
            
        } break;
        case SearchResultSegType_product:{
            
            SearchResultProductViewController *vc = [[SearchResultProductViewController alloc] initWithKeyWord:self.keyWord];
//            vc.resType = SearchProVCType_seg;
            return vc;
            
        } break;
        case SearchResultSegType_post:{
            
            SearchResultPostViewController *vc = [[SearchResultPostViewController alloc] initWithKeyWord:self.keyWord];
//            vc.resType = SearchProVCType_seg;
            return vc;
            
        } break;
        case SearchResultSegType_topic:{
            
            SearchResultTopicViewController *vc = [[SearchResultTopicViewController alloc] initWithKeyWord:self.keyWord];
            return vc;
            
        } break;
        case SearchResultSegType_qa:{
            
            SearchResultQAViewController *vc = [[SearchResultQAViewController alloc] initWithKeyWord:self.keyWord];
            return vc;
            
        } break;
        case SearchResultSegType_baike:{
            
            BaiKeSecondClassesItemVC *vc = [[BaiKeSecondClassesItemVC alloc] init];
            vc.type = BaiKeSecondClassesItemVCTypeSearch;
            [vc realTimeSearchKeyword:self.keyWord];
            return vc;
            
        } break;
            
        default:
            break;
    }

    return [UIViewController new];
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return [self titles][index];
}

- (NSInteger)pageViewControllerNumberOfPage {
    return [self titles].count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {

    NSLog(@"切换到了：%@",[self titles][index]);
}

#pragma mark -
#pragma mark 标题数据
- (NSArray *)titles {
    return @[@"商家",@"套餐",@"案例",@"婚品",@"帖子",@"话题",@"问答",@"百科"];
}

- (SearchNaviBarView *)searchNavi {
    if (!_searchNavi) {
        _searchNavi = [[NSBundle mainBundle] loadNibNamed:@"SearchNaviBarView"owner:nil options:nil].firstObject;
        [_searchNavi.cancleBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
        _searchNavi.searchTF.delegate = self;
    }
    return _searchNavi;
}

@end
